package com.guijq.codesegment.tree;

import java.util.LinkedList;
import java.util.List;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;


public class TreeNodeUtil {


    public static List<TreeNodeVo> transFromTreeNodeBo(List<TreeNodeBo> treeNodeBos, String parentId) {
        List<TreeNodeVo> treeNodeVos = new LinkedList<>();
        for (TreeNodeBo treeNodeBo : treeNodeBos) {
            TreeNodeVo treeNodeVo = new TreeNodeVo();
            treeNodeVo.setNodeName(treeNodeBo.getNodeName());
            treeNodeVo.setObject(treeNodeBo.getObject());
            treeNodeVo.setNodeId(treeNodeBo.getNodeId().toString());
            treeNodeVo.setNodeParentId(parentId);
            if (!CollectionUtils.isEmpty(treeNodeBo.getChildren())) {
                treeNodeVo.setChildren(transFromTreeNodeBo(treeNodeBo.getChildren(), treeNodeVo.getNodeId()));
            }
            treeNodeVos.add(treeNodeVo);
        }
        return treeNodeVos;
    }

    public static String jointNodeId(String parentNodeId, Object nodeId) {
        if (StringUtils.isEmpty(parentNodeId)){
            return nodeId.toString();
        }
        return parentNodeId + "-" + nodeId.toString();
    }

    public static String jointNodeHierarchyId(String parentHierNodeId, String childHierNodeId) {
        return parentHierNodeId + "|" + childHierNodeId;
    }
}
